<?php session_start();

require_once("../config/sina_config.php");
require_once("saetv2.ex.class.php");
require_once("sina_api.php");

include_once('../../../config.inc');
require_once(ROOT.'/database/manage_open_api.php');

function sina_callback()
{
	$_SESSION['callback_count'] ++;
	
	$sae_obj = new SaeTOAuthV2(WB_AKEY, WB_SKEY);
	
	$keys = array();
	$keys['code'] = $_REQUEST['code'];
	$keys['redirect_uri'] = WB_CALLBACK_URL;
	$token = $sae_obj->getAccessToken('code', $keys);
	$access_token = $token['access_token'];
	
	$_SESSION["user_type"] = 1;
	$_SESSION['access_token'] = $access_token;
	$_SESSION["sina_username"] = sina_get_username($access_token);
	
	//setcookie( 'weibojs_'.$sae_obj->client_id, http_build_query($access_token) );
	
	manage_open_login(1, $access_token, "", "", $_SESSION["sina_username"], "");
}

//debug
//print_r($_REQUEST);

sina_callback();

//debug
// print_r($_SESSION);

echo "<script>opener.location.reload();window.close();</script>";
?>